import java.util.Map;

/**
 * @author : zzr
 * @description :二叉搜索树
 * @date :
 */
public class BinarySearchTree {

    static class TreeNode {
        public int val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }
    }

    public TreeNode root = null;

    /**
     * 二 叉搜索树查找方法
     * @param val
     * @return
     */
    public boolean search(int val) {
        TreeNode cur = root;

        while(cur != null) {
            if(val < cur.val) {
                cur = cur.left;
            } else if(val > cur.val) {
                cur = cur.right;
            } else {
                return true;
            }
        }
        return false;
    }

    /**
     * 二叉搜索树的插入操作
     * @param val
     * @return
     */
    public boolean insert(int val) {
        TreeNode node = new TreeNode(val);
        TreeNode cur = root;
        TreeNode parent = null;

        if(root == null) {
            root = node;
        }
        while(cur != null) {
            if(val < cur.val) {
                parent = cur;
                cur = cur.left;
            } else if(val > cur.val) {
                parent = cur;
                cur = cur.right;
            } else {
                return false;
            }
        }

        //cur已经到了要插入结点val应该插入的位置
        if(val > parent.val) {
            parent.right = node;
        } else {
            parent.left = node;
        }
        return true;
    }

    /**
     * 删除的方法：
     * @param val
     */
    public void remove(int val) {
        TreeNode cur = root;
        TreeNode parent = null;
        while(cur != null) {
            if(val < cur.val) {
                parent = cur;
                cur = cur.left;
            } else if(val > cur.val) {
                parent = cur;
                cur = cur.right;
            } else {
                //删除的逻辑
                removeNode(parent,cur);
                return;
            }
        }
    }

    private void removeNode(TreeNode parent, TreeNode cur) {
        if(cur.left == null) {
            if(cur == root) {
                root = cur.right;
            } else if(cur == parent.right) {
                parent.right = cur.right;
            } else {
                parent.left = cur.right;
            }
        } else if(cur.right == null) {
            if(cur == root) {
                root = cur.left;
            } else if(cur == parent.right) {
                parent.right = cur.left;
            } else {
                parent.left = cur.left;
            }
        } else {
            //替代法：
            TreeNode targetParent = cur;
            TreeNode target = cur.right;
            while(target.left != null) {
                targetParent = target;
                target = target.left;
            }
            cur.val = target.val;
            if(targetParent.left == target) {
                targetParent.left = target.right;
            } else {
                targetParent.right = target.right;
            }
        }
    }

}
